package ec.edu.udla.fichamedica.server.controller;

import java.util.LinkedList;
import java.util.List;

import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;

import com.google.appengine.api.datastore.Key;

public class EntityManager<T> {
	/*
	 * private static final Logger log = Logger .getLogger(T.class.getName());
	 */

	private static final EntityManagerFactory pmfInstance = Persistence
			.createEntityManagerFactory("ec.edu.udla.fichamedica");

	private static javax.persistence.EntityManager em = null;

	private static EntityTransaction trx = null;

	private T entity;

	private String query;

	public EntityManager() {
		if (em == null)
			em = get().createEntityManager();
	}

	public static EntityManagerFactory get() {
		return pmfInstance;
	}

	@SuppressWarnings("unchecked")
	public List<T> getResults() {

		List<T> results = new LinkedList<T>();

		try {
			Query query = em.createQuery(this.getQuery());
			query.setParameter(1, 0);
			results = (List<T>) query.getResultList();
			System.out.println("Resultados : " + results.size());
		} catch (Exception e) {
			e.printStackTrace();
		}

		return results;

	}

	public Boolean create(T entity) {
		Boolean success = true;

		try {

			if (trx == null)
				trx = em.getTransaction();

			trx.begin();

			em.persist(entity);
			em.flush();
			
			trx.commit();

			System.out.println("Persist success");

		} catch (Exception e) {
			success = false;
			System.out.println("Persist error");
			e.printStackTrace();
		}

		return success;
	}

	public Boolean update(T entity) {
		Boolean success = true;
		try {
			if (trx == null)
				trx = em.getTransaction();
			trx.begin();
			em.merge(entity);
			em.flush();
			trx.commit();
			System.out.println("Merge success");

		} catch (Exception e) {
			success = false;
			System.out.println("Persist error");
			e.printStackTrace();
		}

		return success;
	}

	public javax.persistence.EntityManager getEm() {
		return em;
	}

	public void setEm(javax.persistence.EntityManager em) {
		this.em = em;
	}

	public Boolean remove(T entity) {
		Boolean success = true;
		EntityTransaction trx = null;

		try {
			if (trx == null)
				trx = em.getTransaction();

			trx.begin();
			em.remove(entity);
			em.flush();
			trx.commit();

			System.out.println("Persist success");

		} catch (Exception e) {
			success = false;
			System.out.println("Persist error");
			e.printStackTrace();
		}

		return success;
	}

	public void setInstance(T entity, Key key) {

		Boolean success = true;
		EntityTransaction trx = null;

		try {
			if (trx == null)
				trx = em.getTransaction();

			this.entity = (T) em.find(entity.getClass(), key);

			// trx.commit();

			System.out.println("Instance to edit : " + this.entity.toString());

		} catch (Exception e) {
			success = false;
			System.out.println("Persist error");
			e.printStackTrace();
		}

	}

	public T getInstance() {
		return this.entity;
	}

	public String getQuery() {
		return query;
	}

	public void setQuery(String query) {
		this.query = query;
	}
}
